package blue.mqtt4j.core.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Jin Zheng
 * @since 2020-10-07
 */
public class MessageIdGen
{
	public static final int MIN = 1;
	public static final int MAX = 65535;

	private static MessageIdGen instance = new MessageIdGen();

	private AtomicInteger counter = new AtomicInteger(MIN);

	private MessageIdGen()
	{
	}

	public static MessageIdGen getInstance()
	{
		return instance;
	}

	public int id()
	{
		counter.compareAndSet(MAX, MIN);
		return counter.getAndIncrement();
	}

}
